package com.geetour.columbus.admin.controller;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.geetour.columbus.admin.service.IAdminInnerService;
import com.geetour.columbus.common.model.LoginUser;
import com.geetour.columbus.common.model.A.Admin;
import com.geetour.columbus.core.filters.LoginFilter;
import com.geetour.columbus.core.thread.ThreadObjManager;

@Controller
public class AdminLoginController {

    private IAdminInnerService adminInnerServiceImpl;

    @Autowired
    public void setAdminInnerServiceImpl(IAdminInnerService adminInnerServiceImpl) {
        this.adminInnerServiceImpl = adminInnerServiceImpl;
    }

    @RequestMapping("/toLogin.jsp")
    public String init() {
        return "/login.html";
    }

    @RequestMapping("/login.jsp")
    public String login(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, LoginUser loginUser)
                                                                                                                         throws IOException {
        String message = null;
        if (loginUser == null) {
            message = "登陆信息不能为空！";
            modelMap.put("message", message);
            return "/login.html";
        }
        HttpSession session = request.getSession();
        String sessionCode = (String) session.getAttribute("validateCode");
        if (null == sessionCode) {
            message = "会话已过期，请重新登录!";
            modelMap.put("message", message);
            return "/login.html";
        }
        if (!loginUser.getCheckcode().toUpperCase().equalsIgnoreCase(sessionCode)) {
            message = "验证码不正确！";
            modelMap.put("message", message);
            return "/login.html";
        }
        try{
            Admin admin = null;
            admin = adminInnerServiceImpl.getAdminByName(loginUser.getUsername());
            if (admin == null) {
                message = "用户名不存在！";
                modelMap.put("message", message);
                return "/login.html";
            }
            if (!admin.getPassword().equals(loginUser.getPassword())) {
                message = "密码错误！";
                modelMap.put("message", message);
                return "/login.html";
            }
            // 设置Session信息
            loginUser.setAdminUuid(admin.getUuid());
            session.setAttribute(LoginFilter.SESSION_LOGINUSER, loginUser);
            ThreadObjManager.setLoginUser(loginUser);
            ThreadObjManager.setCookieUsername(loginUser.getUsername());
            // 设置Cookie
            Cookie usernameCookie = new Cookie("usernameCookie", loginUser.getUsername());
            Cookie passwordCookie = new Cookie("passwordCookie", loginUser.getPassword());
            response.addCookie(usernameCookie);
            response.addCookie(passwordCookie);
            // 更新登录时间
            adminInnerServiceImpl.updateLoginTime(admin.getUuid());
            response.sendRedirect("/index.jsp");
        }catch(Exception e){
            e.printStackTrace();
        }
        return "/error.html";
    }

    @RequestMapping("/logout.jsp")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        // 设置Session失效
        request.getSession().invalidate();
        Cookie usernameCookie = new Cookie("usernameCookie", null);
        // 设置Cookie失效
        Cookie passwordCookie = new Cookie("passwordCookie", null);
        response.addCookie(usernameCookie);
        response.addCookie(passwordCookie);
        System.out.println("logout");
        return "/login.html";
    }

    @RequestMapping("/dashboard.jsp")
    public String dashboard() {
        return "/dashboard.html";
    }

    @RequestMapping("/index.jsp")
    public String index() {
        return "/index.html";
    }
    
    @RequestMapping("/error.jsp")
    public String error() {
        return "/error.html";
    }
}
